package com.yaojiaxun.logisticsPlatformSystem.controller;

import com.yaojiaxun.logisticsPlatformSystem.entity.ClientInfo;
import com.yaojiaxun.logisticsPlatformSystem.entity.OrderInfo;
import com.yaojiaxun.logisticsPlatformSystem.service.ClientInfoService;
import com.yaojiaxun.logisticsPlatformSystem.service.CompanyInfoService;
import com.yaojiaxun.logisticsPlatformSystem.service.OrderInfoService;
import com.yaojiaxun.logisticsPlatformSystem.service.RouteInfoService;
import com.yaojiaxun.logisticsPlatformSystem.util.ResultVOUtil;
import com.yaojiaxun.logisticsPlatformSystem.vo.ResultVO;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Auther: yjx_j
 * @Date: 2019/2/9 19:46
 * @Description:
 */
@Controller
public class OrderInfoController {
    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private CompanyInfoService companyInfoService;

    @Autowired
    private RouteInfoService routeInfoService;

    @Autowired
    private ClientInfoService clientInfoService;

    //发货单填写，先登录
    @RequestMapping("/order_send_before")
    public String orderSendBefore(@RequestParam("routeId") Long routeId, Map<String, Object> map, HttpSession session){
        if (session.getAttribute("user") == null){
            return "front/log/log_login";
        }
        map.put("route", routeInfoService.findRouteInfo(routeId));
        return "front/order/order_send_before";
    }
    //发货单填写后
    @RequestMapping("/order_send_after")
    public String orderSendAfter(OrderInfo orderInfo, Map<String, Object> map, HttpSession session){
        orderInfoService.insertOrderInfo(orderInfo);
        map.put("comInfo",companyInfoService.findCompanyByName(orderInfo.getOrderCom()));
//        return "system/com/com_detail_other";
        return "redirect:/clientCenterOrderUnfinished?clientAccount="+session.getAttribute("user");
    }

    //根据orderId获取订单信息以Json返回给驻平台管理员管理页面的模态框
    @ResponseBody
    @RequestMapping("/getOrderById")
    public ResultVO getOrderById(@RequestParam("orderId") Long orderId){
        return ResultVOUtil.success(orderInfoService.findOrderByOrderId(orderId));
    }
    //根据orderId返回下单客户的信息
    @ResponseBody
    @RequestMapping("/getClientInfoByOrderId")
    public ResultVO getClientInfoByOrderId(@RequestParam("orderId") Long orderId){
        String clientAccount = orderInfoService.findOrderByOrderId(orderId).getOrderClient();
        return ResultVOUtil.success(clientInfoService.findClientByClientAccount(clientAccount));
    }

    //客户点击确认订单
    @ResponseBody
    @RequestMapping("/confirmOrder")
    public ResultVO confirmOrder(@RequestParam("orderId") Long orderId){
        OrderInfo orderInfo = orderInfoService.findOrderByOrderId(orderId);
        orderInfo.setOrderStatus("已完成");
        return ResultVOUtil.success(orderInfoService.updateOrderInfo(orderInfo));
    }
    //根据orderId找客户账号
    @ResponseBody
    @RequestMapping("/findOrderClientByOrderId")
    public ResultVO findOrderClientByOrderId(@RequestParam("orderId") Long orderId){
        return ResultVOUtil.success(orderInfoService.findOrderByOrderId(orderId).getOrderClient());
    }
    //客户删除订单
    @ResponseBody
    @RequestMapping("/clientDeleteOrderByOrderId")
    public ResultVO clientDeleteOrderByOrderId(@RequestParam("orderId") Long orderId){
        orderInfoService.deleteOrderInfo(orderId);
        return ResultVOUtil.success();
    }
}
